﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class setTurn : FunctionBase
    {
        private MarsExplorerEntities dbContext;
        public int TurnId { get; set; }
        int combustivel = 0, oxigenio = 0, comida = 0, agua = 0, medico = 0, escudo = 0,fato=0;
        int _combustivel = 0, _oxigenio = 0, _comida = 0, _agua = 0, _medico = 0, _escudo = 0,_fato=0;
        public setTurn(HttpContext context)
            : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            Game g =( from x in dbContext.Games where x.GameID == 1 select x).FirstOrDefault();
          
            List<Team> t = (from z in dbContext.Teams select z).ToList();
            foreach (Team a in t)
            {
                if (a.canMove ==0 || a.Mining==1)
                {
                    checkMoveAllowed(a.TeamID, a.Mining);
                    if (fato>0)
                    {
                        a.Credits += 1000000;
                    }
                    removeResources(a.TeamID);
                	
                }
                a.canMove = 0;
                
            }
            g.RunningTurn = TurnId;
            dbContext.SaveChanges();
           
                return "OK";
          
        }
       

        private string getNeededResources(int TeamID,int? mining)
        {
            string teamRoute = (from x in dbContext.Teams where x.TeamID == TeamID select x.Route.RouteType).FirstOrDefault();
            Game g = (from d in dbContext.Games where d.GameID == 1 select d).FirstOrDefault();
            GamesEvent ge = (from z in dbContext.GamesEvents where z.GameID == "1" && z.Route == teamRoute && z.GameTurn==g.RunningTurn select z).FirstOrDefault();

            if (ge != null)
            {
                switch (ge.EventID)
                {
                    case 1:  //explosao solar

                        _agua = 2;
                        _comida = 2;
                        _oxigenio = 2;
                        _combustivel = 2;
                        _medico = 1;
                        _escudo = 1;
                        break;
                    case 2: //meteoritos

                        _agua = 1;
                        _comida = 1;
                        _oxigenio = 3;
                        _combustivel = 3;
                        _medico = 0;
                        _escudo = 0;
                        break;
                }
            }
            else
            {
                if (teamRoute !="P")
                {
                    _agua = 1;
                    _comida = 1;
                    _oxigenio = 1;
                    _combustivel = 1;
                    _medico = 0;
                    _escudo = 0;
                }
                else
                {
                    if (mining==0)
                    {
                        _agua = 1;
                        _comida = 1;
                        _oxigenio = 1;
                        _combustivel = 1;
                        _medico = 0;
                        _escudo = 0;
                    }
                    else
                    {
                        _agua = 1;
                        _comida = 1;
                        _oxigenio = 1;
                        _combustivel = 1;
                        _medico = 0;
                        _escudo = 0;
                        _fato = 1;
                    }
                }
                

            }
            return "ok";
        }

        private string getAllocatedResources(int TeamID)
        {
            var tt = from tts in dbContext.TeamsTurns where tts.TeamID == TeamID select tts;

            foreach (TeamsTurn j in tt)
            {
                switch (j.ResourceID)
                {
                    case 1:
                        agua = j.Value;
                        break;
                    case 2:
                        combustivel = j.Value;
                        break;
                    case 3:
                        comida = j.Value;
                        break;
                    case 4:
                        oxigenio = j.Value;
                        break;
                    case 5:
                        medico = j.Value;
                        break;
                    case 7:
                        escudo = j.Value;
                        break;
                    case 8:
                        fato = j.Value;
                        break;
                    default:
                        break;
                }
            }
            return "ok";
        }

        private Boolean checkMoveAllowed(int TeamID,int? mining)
        {
            getNeededResources(TeamID,mining);
            getAllocatedResources(TeamID);

            if (agua < _agua)
            {
                agua = _agua;
            }
            if (combustivel < _combustivel)
            {
                combustivel = _combustivel;
            }
            if (oxigenio < _oxigenio)
            {
                oxigenio = _oxigenio;
            }
            if (comida < _comida)
            {
                comida = _comida;
            }
            if (medico < _medico)
            {
                medico = _medico;
            }
            if (escudo < _escudo)
            {
                escudo = _escudo;
            }
            if (fato < _fato)
            {
                fato = _fato;
            }
            return true;
           
        }
        private string removeResources(int TeamID)
        {
            try
            {
                var tr = (from x in dbContext.TeamsResources where x.TeamID == TeamID select x).ToList();
                var tt = (from z in dbContext.TeamsTurns where z.TeamID == TeamID select z).ToList();
                foreach (TeamsTurn f in tt)
                {
                    f.Value = 0;
                }
                foreach (TeamsResource r in tr)
                {
                    switch (r.ResourceID)
                    {
                        case 1:
                            r.Value -= agua;
                            break;
                        case 2:
                            r.Value -= combustivel;
                            break;
                        case 3:
                            r.Value -= comida;
                            break;
                        case 4:
                            r.Value -= oxigenio;
                            break;
                        case 5:
                            r.Value -= medico;
                            break;

                        case 7:
                            r.Value -= escudo;
                            break;

                        case 8:
                            r.Value -= fato;
                            break;
                        default:
                            break;
                    }
                    dbContext.SaveChanges();
                }
                return "ok";
            }
            catch (Exception e)
            {

                return "error";
            }

        }
        public override void InitializeProperties()
        {
            try
            {
                TurnId = Convert.ToInt32(Parameters["turnid"]);
                dbContext = new MarsExplorerEntities();
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }
}